// pages/cash/withdraw/withdraw.js
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();

// 引入tDesign消息提示组件
import Message from 'tdesign-miniprogram/message/index';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        barHeight: App.globalData.paddingTop,

        styledata: {
            "bg_color": "white",
            "color": "#000",
            "leftflag": true,
            // "homeflag": true,
            "name": "余额提现"
        },

        // 可提现余额
        availableMoney: "0.00",

        isForbid: false,
        // 提现中标识
        withdrawing: false,
        textfont: "发送验证码"

    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        await this.initData()
        this.queryBalance()
    },

    async initData(options) {
        await App.getMyFullInfo()
        this.setData({
            userInfo: App.globalData.fulluserInfo,
            isVip: App.globalData.isVip,
            // isVip: true,
            uid: App.globalData.uid
            // uid: 7486
            // uid: 7455
        })
    },

    // 提交数据生成提现订单 并获取手机验证码
    async applyWithdrawcode() {

        console.log("wDetail", this.wDetail);
        let uid = this.data.uid
        let amount = this.data.wAmount

        if (!uid || !amount) {
            return this.showErrorMessage("请输入提现金额")
        }

        let params = {
            uid,
            amount
        }

        console.log(params);
        let codeRes = await request.postall(api.addwithdraworder, params)
        console.log(codeRes);
        if (codeRes.code == 0) {
            // 保存提现订单Id
            this.wId = codeRes.data
            this.applyWithdraw(codeRes.data)
            // this.showSuccessMessage(codeRes.msg)
            return true
        } else {
            this.showErrorMessage(codeRes.msg)
            return false
        }
    },

    // 利用提现订单ID 发起提现
    async applyWithdraw(Id) {
        let withdraw = await request.postall(api.withdrawApply, { id: Id })
        console.log(withdraw);
        if (withdraw.code == 0) {
            // 验证码发送成功 限制发送间隔
            this.changetip()
            this.showSuccessMessage(withdraw.msg)
            return true
        } else {
            this.showErrorMessage(withdraw.msg)
            return false
        }
    },

    // 填写验证码后 确认提现
    async confirmWithdraw() {
        let amount = this.data.wAmount
        if (!amount) {
            this.showErrorMessage("请输入提现金额")
            return
        }
        let wCode = this.data.wCode
        console.log(wCode);
        let params = {
            id: this.wId,
            code: wCode
        }
        let withdrawRes = await request.postall(api.confirmWithdraw, params)
        console.log(withdrawRes);

        if (withdrawRes.code == 0) {
            this.showSuccessMessage(withdrawRes.msg)

            // 提示完返回上一页面
            let timer = setTimeout(() => {
                clearTimeout(timer)
                wx.navigateBack({
                    delta: 1
                });
            }, 2500);
            return true
        } else {
            this.showErrorMessage(withdrawRes.msg)
            return false
        }

    },

    // 倒数提示
    changetip(num = 60) {
        this.setData({
            isForbid: true
        })
        let timer = setInterval(() => {
            if (num == 1) {
                this.setData({
                    isForbid: false,
                    textfont: `发送验证码`
                })
                clearInterval(timer)
                return
            }

            num--
            this.setData({
                textfont: `重新发送${num}`
            })
        }, 1000);
    },


    // 查询会员提现余额
    async queryBalance() {
        let Balance = await request.post(api.checkAllinBalance, { uid: this.data.uid })
        let allAmount = (Balance.allAmount / 100).toFixed(2)
        // console.log("余额", Balance, allAmount);
        this.setData({
            availableMoney: allAmount
        })
    },

    // 获取对应的输入框的输入值
    inputBlur(e) {
        let valname = e.currentTarget.dataset.valname
        this.setData({
            [`${valname}`]: e.detail.value
        })
    },

    // 错误提示
    showErrorMessage(content) {
        Message.error({
            context: this,
            offset: [144, 130],
            duration: 2500,
            content: content,
        });
    },
    // 成功提示
    showSuccessMessage(content) {
        Message.success({
            context: this,
            offset: [144, 130],
            duration: 2500,
            content: content,
        });
    },

    // 金额输入框点击事件
    handleclick() {
        this.setData({
            wAmount: this.data.availableMoney
        })
        console.log(88888888888);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})